-
-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
updates readme for nestjs #142
base: master
Are you sure you want to change the base?
Conversation
IMO, this is all knowledge not specific to aws-lambda-fastify, but general to how AWS Lambda works in combination with NestJS... |
I would say it's about how aws-lambda-fastify works in combination with NestJS, not just generic AWS Lambda. But, I do see your point; however, there is an open issue inquiring about this which led to some more discussion and someone asked me to update the readme in order to persist the knowledge. Maybe that's not the right place for it, though. |
I think a better place for this documentation is on the Nest.js website. |
That's exactly what I meant 😉 |
@mcollina I agree. I'll see about getting this added to their documentation. |
@Uzlopak ok for you if we close this PR in favor to document it on the Nest.js side somewhere? |
I am totally ok with it as long it is somewhere documented?! Is it likely that nest js will accept that additional documentation? |
Just want to say that I was looking around today and was very happy to find a PR here with documentation about using Next.js with Fastify on Lambda. Is there a PR for Nest.js documentation I can upvote or whatever? I really appreciate being able to find some documentation somewhere on this so I hope it gets out there on the web somewhere somehow... |
@Jwagner347 I see some issues with the code in the README. Mostly variable names and types not lining up because the code is combined. Here's how it should look: import { NestFactory } from '@nestjs/core';
import {
FastifyAdapter,
NestFastifyApplication,
} from '@nestjs/platform-fastify';
import { AppModule } from './app.module';
import awsLambdaFastify, { PromiseHandler } from '@fastify/aws-lambda';
import fastify, { FastifyInstance, FastifyServerOptions } from 'fastify';
import { Context, APIGatewayProxyEvent } from 'aws-lambda';
import { Logger } from '@nestjs/common';
interface NestApp {
app: NestFastifyApplication;
instance: FastifyInstance;
}
let cachedNestApp;
async function bootstrap(): Promise<NestApp> {
const serverOptions: FastifyServerOptions = {
logger: (process.env.LOGGER || '0') == '1',
};
const instance: FastifyInstance = fastify(serverOptions);
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter(instance),
{
logger: !process.env.AWS_EXECUTION_ENV ? new Logger() : console,
},
);
const CORS_OPTIONS = {
origin: '*',
allowedHeaders: '*',
exposedHeaders: '*',
credentials: false,
methods: ['GET', 'PUT', 'OPTIONS', 'POST', 'DELETE'],
};
app.register(require('fastify-cors'), CORS_OPTIONS);
app.setGlobalPrefix(process.env.API_PREFIX);
await app.init();
return {
app,
instance,
};
}
export const handler = async (
event: APIGatewayProxyEvent,
context: Context,
): Promise<PromiseHandler> => {
if (!cachedNestApp) {
const nestApp: NestApp = await bootstrap();
cachedNestApp = awsLambdaFastify(nestApp.instance, {
decorateRequest: true,
});
}
return cachedNestApp(event, context);
}; However, we have a bigger issue and that is a type mismatch when adapting Fastify:
I'll see if I can figure it out and update below. |
I submitted an bug report issue here. |
OK I took another crack at fixing it using the Fastify instance from the
|
I got it working! The code that works is as follows:
I had to make sure that the Fastify version was the same between what was installed here and what |
This PR updates the Readme with instructions on running with NestJS and with NestJS/GraphQL. This should also close out #115.
Checklist
npm run test
andnpm run benchmark
and the Code of conduct